<?php
/**
 ***** 扶摇而上、抱守本真 ******
 * @author Dana
 * @Date 2021/6/30
 */

if(!(filter_has_var(INPUT_POST,'first_name') &&
    (strlen(filter_input(INPUT_POST,'first_name')) > 0))){
    echo "you must enter you first_name<br>";
}

echo "hello,".$_POST['first_name'];


$email = filter_input(INPUT_POST,'first_name',FILTER_VALIDATE_EMAIL);
if($email === false){
    print "submitted email address is invalid<br>";
}



// 检查到期时间

// 信用卡到期月份的起始日期
$expires = mktime(0,0,0,8,1,2021);
$lastMonth = strtotime('last month',$expires);
if(time() > $lastMonth){
    print "sorry,that credit card expires too soon.<br>";
}

// 验证表单输入：信用卡

function is_valid_credit_card($credit){
    // 删除非数字字符，并反向
    $credit = strrev(preg_replace('/[^\d]/','',$credit));
    //6216 6120 0001 0277 081
    var_dump($credit);
    // 计算校验和
    $sum = 0;
    for($i = 0,$j = strlen($credit); $i < $j;$i++){
        // 偶数未不做修改
        if(($i % 2) ==0){
            $val = $credit[$i];
        }else{
            $val = $credit[$i] * 2;
            if($val > 9){
                $val -=9;
            }
        }
        $sum +=$val;
    }
    return (($sum % 10) == 0);
}

if(!is_valid_credit_card($_POST['credit_card'])){
    print 'sorry,that card number is invalid.<br>';
}

function create_credit_number(){

}

